<template>
  <div class="data-form goods-activity-form" v-loading.fullscreen.lock="loading">
    <el-form label-width="auto" size="medium">
      <el-form-item label="活动类型">
        <el-select v-model="activityForm.activityTypeId" class="form-input">
          <el-option v-for="item in activityTypeList" :key="item.id" :value="item.id" :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属总店">
        <el-select filterable v-model="activityForm.branchId" class="form-input" :disabled="inputLoading" @change="branchChange">
          <el-option v-for="branch in branchList" :key="branch.branchId" :value="branch.branchId" :label="branch.branchName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属分店">
        <el-select filterable clearable v-model="activityForm.partnerId" class="form-input" :disabled="inputLoading" @change="partnerChange">
          <el-option v-for="partner in partnerList" :key="partner.id" :value="partner.id" :label="partner.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动商品">
        <el-select filterable v-model="activityForm.goodsDefId" class="form-input" :disabled="inputLoading">
          <el-option v-for="item in goodsList" :key="item.id" :value="item.id" :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <sys-date-time-picker v-model="activityForm.activityStartTime" class="form-input" type="hour"></sys-date-time-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <sys-date-time-picker v-model="activityForm.activityEndTime" class="form-input" type="hour"></sys-date-time-picker>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="activityForm.status" class="form-input">
          <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button size="medium" type="primary" @click="saveActivity">
        <span v-if="type==='add'">新增</span>
        <span v-else>修改</span>
      </el-button>
    </div>
  </div>
</template>

<script>
  import GoodsActivityForm from 'models/goods/goodsActivityForm';

  import SysDateTimePicker from 'commonComponents/sysDateTimePicker';

  import PermissionApi from 'services/permissionApi';
  import GoodsApi from 'services/goodsApi';

  export default {
    props: {
      activity: {
        type: Object,
        default () {
          return null
        }
      },
      type: {
        type: String,
        default: 'add'
      },
      dropdownOption: {
        type: Object,
        default () {
          return {};
        }
      }
    },

    data() {
      let activityForm;
      if (this.type === 'add') {
        activityForm = new GoodsActivityForm();
      } else {
        activityForm = _.cloneDeep(this.activity);
      }

      let dropdownOption = this.dropdownOption;
      return {
        loading: false,
        inputLoading: false,
        activityForm,
        activityTypeList: dropdownOption.activityList,
        branchList: dropdownOption.branchList,
        partnerList: [],
        goodsList: [],
        statusList: this.i18n.getDropDown('goods.goodsActivity.status', [1, 0])
      };
    },

    methods: {
      branchChange() {
        this.inputLoading = true;
        Promise.all([
          this.setPartnerList(),
          this.setGoodsList('branch')
        ]).then(() => {
          this.activityForm.partnerId = '';
          this.inputLoading = false;
        }).catch(() => {
          this.inputLoading = false;
        });
      },

      partnerChange() {
        this.inputLoading = true;
        this.setGoodsList('partner').then(() => {
          this.inputLoading = false;
        }).catch(() => {
          this.inputLoading = false;
        });
      },

      setPartnerList() {
        let branchId = this.activityForm.branchId;
        if (branchId === '') {
          this.partnerList = [];
          return Promise.resolve([]);
        }
        return PermissionApi.getPartners(branchId).then(partnerList => {
          this.partnerList = partnerList;
          return partnerList;
        });
      },

      setGoodsList(type) {
        return GoodsApi.getGoodsDropdown(this.activityForm, type).then(goodsList => {
          let id = this.activityForm.goodsDefId;
          let goods = _.find(goodsList, {
            id: id
          });
          if (goods === undefined) {
            this.activityForm.goodsDefId = '';
          }

          this.goodsList = goodsList;
          return goodsList;
        });
      },

      saveActivity() {
        let activityForm = this.activityForm;
        let code = activityForm.checkValid();
        if (code !== '') {
          let msg = this.i18n.getMsg(`goods.goodsActivity.error.${code}`);
          this.$message(msg);
          return;
        }

        if (this.type === 'add') {
          this.addActivity(activityForm);
        }
      },

      addActivity(activityForm) {
        this.loading = true;
        GoodsApi.addGoodsActivity(activityForm).then(() => {
          this.loading = false;
          this.$alert('商品活动新增成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      }
    },

    components: {
      SysDateTimePicker
    }
  };

</script>

<style lang="scss" scoped>
  @import '~viewStyle/dataForm';

</style>
